package anc.tankshooter.network.ui;

import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.color.Color;

import anc.server.tankshooter.models.Account;
import anc.tankshooter.GameActivity;
import anc.tankshooter.controls.InputText;
import anc.tankshooter.ui.IScreen;
import anc.tankshooter.ui.ResourcesManager;
import anc.tankshooter.ui.SceneManager.SceneType;
import anc.tankshooter.ui.SettingScreen.SettingButtonListener;

/**
 * Man hinh dang nhap SERVER
 * @author tauitdnmd
 *
 */
public class SignUpScreen extends IScreen {

	private SettingButtonListener mlistener;
	private Rectangle mBackgroundRect;
	private Sprite mBackgroundBox;
	private Text mMessageText,mUserText,mPassText,mNameText,mEmailText;
	private InputText mUsername,mPassword,mName,mEmail;
	private TiledSprite mBtnRegister,mBtnCancle;
	
	public Account mAccount;
	private boolean mIsRunning;
	
	public SignUpScreen(SettingButtonListener l){
		mlistener = l;
		mAccount = new Account();
	}
	@Override
	protected void createScene() {
		mBackgroundRect = new Rectangle(0, 0, GameActivity.CAMERA_WITDH, GameActivity.CAMERA_HEIGHT,ResourcesManager.getInstance().mVertexManager);
		mBackgroundRect.setColor(Color.BLACK); mBackgroundRect.setAlpha(0.6f);
		
		float x = GameActivity.CAMERA_WITDH/2 - ResourcesManager.getInstance().mPanelTexture.getWidth()/2;
		float y = GameActivity.CAMERA_HEIGHT/2 - 450/2;
		
		mBackgroundBox = new Sprite(x, y, ResourcesManager.getInstance().mPanelTexture, mVertextManager);
		mBackgroundBox.setHeight(450f);
		
		y+=50;
		mMessageText = new Text(260,y-30, ResourcesManager.getInstance().mWhiteFont15, "", 256,ResourcesManager.getInstance().mVertexManager);
		
		mUserText = new Text(260,y+40, ResourcesManager.getInstance().mWhiteFont18, "Username",ResourcesManager.getInstance().mVertexManager);
		mUsername = new InputText(260f,y+60, "Your username",ResourcesManager.getInstance().mTextBoxTexture, ResourcesManager.getInstance().mWhiteFont20,10);
		
		mPassText = new Text(260,y+110, ResourcesManager.getInstance().mWhiteFont18, "Password",ResourcesManager.getInstance().mVertexManager);
		mPassword = new InputText(260f, y+130, "Your password",ResourcesManager.getInstance().mTextBoxTexture, ResourcesManager.getInstance().mWhiteFont20,15);
		
		mNameText = new Text(260,y+180, ResourcesManager.getInstance().mWhiteFont18, "Name",ResourcesManager.getInstance().mVertexManager);
		mName = new InputText(260f,y+200, "Your name",ResourcesManager.getInstance().mTextBoxTexture, ResourcesManager.getInstance().mWhiteFont20,50);
		
		mEmailText = new Text(260,y+250, ResourcesManager.getInstance().mWhiteFont18, "Email",ResourcesManager.getInstance().mVertexManager);
		mEmail = new InputText(260f, y+270, "Your email",ResourcesManager.getInstance().mTextBoxTexture, ResourcesManager.getInstance().mWhiteFont20,50);
		
		mBtnRegister = new TiledSprite(300,y+330,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnRegister.setCurrentTileIndex(14);
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnRegister.setCurrentTileIndex(15);
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP){
					mBtnRegister.setCurrentTileIndex(14);
					if(checkForm())
						doRegister();
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mBtnRegister.setCurrentTileIndex(14);
		
		mBtnCancle = new TiledSprite(300 + ResourcesManager.getInstance().mBtnTexture.getWidth()+10,y+330,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnCancle.setCurrentTileIndex(8);
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnCancle.setCurrentTileIndex(9);
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP){
					mBtnCancle.setCurrentTileIndex(8);
					if(mlistener!=null)
						mlistener.onCancleClicked();
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mBtnCancle.setCurrentTileIndex(8);
		
		attachChild(mBackgroundRect);
		attachChild(mBackgroundBox);
		attachChild(mMessageText);
		
		attachChild(mUserText);
		attachChild(mUsername); registerTouchArea(mUsername);
		
		attachChild(mPassText);
		attachChild(mPassword); registerTouchArea(mPassword);
		
		attachChild(mNameText);
		attachChild(mName); registerTouchArea(mName);
		
		attachChild(mEmailText);
		attachChild(mEmail); registerTouchArea(mEmail);
		
		attachChild(mBtnRegister); registerTouchArea(mBtnRegister);
		
		attachChild(mBtnCancle); registerTouchArea(mBtnCancle);
		
	}

	@Override
	public boolean onBackKeyPressed() {
		
		return true;
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_LOGIN;
	}

	@Override
	public void disposeScene() {
		
	}

	@Override
	public void onUpdateScreen(float second) {		
	}

	private boolean checkForm(){
		StringBuffer sb = new StringBuffer();
		mAccount.setUsername( mUsername.getText());
		mAccount.setPassword(mPassword.getText());
		mAccount.setName(mName.getText());
		mAccount.setEmail(mEmail.getText());
		
		if(mAccount.getUsername()==null||mAccount.getUsername().length()<=0)
			sb.append("Username is empty!").append("\n");
		if(mAccount.getPassword()==null||mAccount.getPassword().length()<=0)
			sb.append("Password is empty!").append("\n");
		if(mAccount.getName()==null||mAccount.getName().length()<=0)
			sb.append("Name is empty!").append("\n");
		if(mAccount.getEmail()==null||mAccount.getEmail().length()<=0)
			sb.append("Email is empty!");
		
		mMessageText.setText(sb.toString());
		return sb.length()<=0;
	}
	
	private void doRegister(){
		if(!mIsRunning) {
			mBtnRegister.setCurrentTileIndex(15);
			mMessageText.setText("Sending...");
			mAccount.setUsername( mUsername.getText());
			mAccount.setPassword(mPassword.getText());
			mAccount.setName(mName.getText());
			mAccount.setEmail(mEmail.getText());
			
			Thread th = new Thread(new Runnable() {
				@Override
				public void run() {
					mIsRunning = true;
					mUsername.setEnable(false);
					mUsername.setEnable(false);
					
					if(!GameActivity.mClient.sendSignUp(mAccount)) {
						mMessageText.setText("The request was not sent.");
					}
					mMessageText.setText("");
					mUsername.setEnable(true);
					mUsername.setEnable(true);
					mBtnRegister.setCurrentTileIndex(14);
					mIsRunning = false;
				}
			});
			th.start();
		}
			
	}
}
